package org.lanqiao.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.AnnounceEntity;
import org.lanqiao.tjut.bean.ClientEntity;
import org.lanqiao.tjut.bean.PaysEntity;
import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class ClientControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;

	
	@RequestMapping("/clientquery.do")
	public ModelAndView doQuery(HttpSession session, ClientEntity cliE,UsersEntity userE) {
		System.out.println("查询用户名：" + cliE.getClientname());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		List<ClientEntity> lstUser = userService.selectClientInfoByParas(cliE,userE);
		if (lstUser != null && lstUser.size() > 0) {
			// 登录成功

			// 登录验证成功，将用户信息进行session缓存
			session.setAttribute("QueryClientInfo", lstUser);
			// 设置逻辑视图名--登录成功
			model.setViewName("clientdoQuery");
		} else {
			// 设置逻辑视图名--登录验证失败
			model.setViewName("error");
		}
		return model;
	}

	@RequestMapping("/clientEdit.do")
	public ModelAndView doEdit(HttpSession session, ClientEntity cliE, String uid) {
		System.out.println("client编辑");
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();

		cliE = userService.getClientInfoById(uid);

		// 登录验证成功，将用户信息进行session缓存
		session.setAttribute("cliE", cliE);
		// 设置逻辑视图名--登录成功
		model.setViewName("clientAdd");

		return model;
	}

	
	@RequestMapping("/clientAdd.do")
	public ModelAndView doAdd(HttpSession session, ClientEntity cliE) {
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
			int i = userService.editClientInfo(cliE);
			if (i > 0) {
				// 添加成功
				List<ClientEntity> lstUser = userService.selectClientInfo(cliE);
				// 登录验证成功，将用户信息进行session缓存
				session.setAttribute("QueryClientInfo", lstUser);
				// 设置逻辑视图名--登录成功
				model.setViewName("clientdoQuery");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("error");
			}
		
		return model;
	}
	
	@RequestMapping("/announceAdd.do")
	public ModelAndView doannounceAdd(HttpSession session, AnnounceEntity anE) {
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
			int i = userService.addAnnounceInfo(anE);
			if (i > 0) {
				// 添加成功
				List<AnnounceEntity> anninfo = userService.contentInfo();
				session.setAttribute("QueryAnnInfo", anninfo);
				List<AnnounceEntity> annallinfo = userService.contentAllInfo();
				session.setAttribute("QueryAnnAllInfo", annallinfo);
				// 登录验证成功，将用户信息进行session缓存
				// 设置逻辑视图名--登录成功
				model.setViewName("viewAnnounce");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("error");
			}
		
		return model;
	}
	
	@RequestMapping("/operaPay.do")
	public ModelAndView doAdd(HttpSession session, PaysEntity payE) {
		
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();

			int i = userService.savePayInfo(payE);

			if (i > 0) {
				// 添加成功
				List<PaysEntity> lstPay = userService.selectPayInfo(payE);
				// 登录验证成功，将用户信息进行session缓存
				session.setAttribute("QueryPayInfo", lstPay);
				// 设置逻辑视图名--登录成功
				model.setViewName("PayManager");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("error");
			}
		 
		return model;
	}
	
	
}
